package org.apache.struts.ehr.patient_details.action;

import java.util.GregorianCalendar;
import java.util.Map;

import org.apache.struts.ehr.medical_records.model.MergePatientLog;
import org.apache.struts.ehr.medical_records.model.ReplyMessage;
import org.apache.struts.ehr.patient_details.model.HL7Message;

import com.earogya.Constants;
import com.earogya.communicate.Communicator;
import com.earogya.logic.medical_records.CreateADT34;
import com.earogya.logic.medical_records.ParseReply;
import com.earogya.logic.patient_details.CreateADTA34;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * Action for MergePatient
 * 
 * @author Ureka
 * 
 */
public class MergePatientAction extends ActionSupport {

	private HL7Message message;
	private String patientId1;
	private String patientId2;
	private int realPatientId;
	private int temporaryPatientId;
	private ReplyMessage replyMessage1;
	private ReplyMessage replyMessage2;


	/**
	 * 
	 * Override the execute method for Merge patient
	 * 
	 * return SUCCESS or ERROR
	 * 
	 */
	public String execute() throws Exception {
		ParseReply parse = new ParseReply();
		boolean isSuccess = false;
		String reply=null;
		String reply2=null;
		try{
		Communicator c = new Communicator();
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (Integer.parseInt(patientId1) > Integer.parseInt(patientId2)) {
			temporaryPatientId = Integer.parseInt(patientId1);
			realPatientId = Integer.parseInt(patientId2);
		} else if (Integer.parseInt(patientId1) < Integer.parseInt(patientId2)) {
			temporaryPatientId = Integer.parseInt(patientId2);
			realPatientId = Integer.parseInt(patientId1);
		}

		if (realPatientId != temporaryPatientId) {
			CreateADT34 createMessage = new com.earogya.logic.medical_records.CreateADT34(
					new MergePatientLog(realPatientId, temporaryPatientId,
							new GregorianCalendar().getInstance().getTime(),
							"Nurse"));
			String msg = createMessage.createMessage(Constants.SENDING_FACILITY);
			message = new HL7Message();
			if (!msg.equals(null)) {
				
				reply = c.sendMessage(msg);
				
				isSuccess = true;
				if (isSuccess == true) {
					CreateADTA34 createMessage2 = new com.earogya.logic.patient_details.CreateADTA34(
							temporaryPatientId, realPatientId);
					String msg2 = createMessage2.createMessage(Constants.SENDING_FACILITY);
					message = new HL7Message();
					if (!msg.equals(null)) {
						reply2 = c.sendMessage(msg2);
					} else {
						message.setMessage(Constants.EMPTY);
					}
					replyMessage1 = (ReplyMessage) parse.parseMessage(reply);
					replyMessage2 = (ReplyMessage) parse.parseMessage(reply2);
					if((replyMessage1.getMessage().equals(Constants.SUCCESS) || replyMessage1.getMessage().equals("No patient admitted with the temporary id!" )&& replyMessage2.getMessage().equals(Constants.SUCCESS))){
						replyMessage1.setMessage(Constants.EMPTY);
						replyMessage2.setMessage("Successfully merged patient id "+realPatientId+" and patient id "+temporaryPatientId);
					}
					else if(replyMessage1.getMessage().equals(Constants.SUCCESS) && !(replyMessage2.getMessage().equals(Constants.SUCCESS))){
						replyMessage1.setMessage(Constants.EMPTY);
					}else if(!(replyMessage1.getMessage().equals(Constants.SUCCESS)) && replyMessage2.getMessage().equals(Constants.SUCCESS)){
						replyMessage2.setMessage(Constants.EMPTY);
					}
				}
			} else {
				message.setMessage(Constants.EMPTY);
			}

		}
		}
		catch(ClassCastException e){
			replyMessage1 = (ReplyMessage) parse.parseMessage(reply);
			if(replyMessage1.equals(Constants.SUCCESS)){
				replyMessage1.setMessage(Constants.EMPTY);
			}
			replyMessage2 = (ReplyMessage) parse.parseMessage(reply2);
			if(replyMessage2.equals(Constants.SUCCESS)){
				replyMessage2.setMessage(Constants.EMPTY);
			}
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * 
	 * Validate the user input
	 * 
	 */
	public void validate() {
		if (this.patientId1.length() == 0) {
			addFieldError("patientId1", "Patient ID 1 required");
		} else if (!this.isInteger(this.patientId1)) {
			addFieldError("patientId1", "Incorrect patient ID format.");
		}
		if (this.patientId2.length() == 0) {
			addFieldError("patientId2", "Patient ID 2 required");
		} else if (!this.isInteger(this.patientId2)) {
			addFieldError("patientId1", "Incorrect patient ID format.");
		}
		if (this.patientId1.length() != 0 && this.patientId2.length() != 0) {
			if (this.patientId2.equalsIgnoreCase(this.patientId1)) {
				addFieldError("patientId2",
						"Different Patient IDs required for merge");
			}
		}
	}

	/**
	 * 
	 * Check whether the given String is an integer
	 * 
	 * @param value
	 * @return
	 */
	private Boolean isInteger(String value) {
		Boolean isInteger = false;
		try {
			int intValue = Integer.parseInt(value);
			isInteger = true;
		} catch (NumberFormatException e) {
			isInteger = false;
		}
		return isInteger;
	}

	public String getPatientId1() {
		return patientId1;
	}

	public void setPatientId1(String patientId1) {
		this.patientId1 = patientId1;
	}

	public String getPatientId2() {
		return patientId2;
	}

	public void setPatientId2(String patientId2) {
		this.patientId2 = patientId2;
	}

	public HL7Message getMessage() {
		return message;
	}

	public void setMessage(HL7Message message) {
		this.message = message;
	}

	public ReplyMessage getReplyMessage1() {
		return replyMessage1;
	}

	public void setReplyMessage1(ReplyMessage replyMessage1) {
		this.replyMessage1 = replyMessage1;
	}

	public ReplyMessage getReplyMessage2() {
		return replyMessage2;
	}

	public void setReplyMessage2(ReplyMessage replyMessage2) {
		this.replyMessage2 = replyMessage2;
	}

	
}
